const router=require('koa-router')();
const config=require('../config');
//const redis=require('redis');//使用缓存
const gzhUtil=require('../util/gzhUtil');
router.prefix('/api/gzh/login');
//const client=redis.createClient("6379","127.0.0.1");

/**
 * @api {post} /api/gzh/login/getUrl 拼接跳转到微信服务器的链接
 * @apiName getUrl
 * @apiGroup login
 *
 * @apiParam {String} url 当前页面的url
 *
 * @apiSuccess {Object[]} data
 * @apiSuccess {String} data.url 跳转到微信服务器的链接
 */
router.post('/getUrl',async (ctx)=>{
   try{
       let param=ctx.request.body;
       let paramUrl=param.url;
       if(!paramUrl){
           const result={
               code:4,
               msg:'参数不全'
           };
           ctx.body=result;
           return;
       }
       let gzhUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=cbl123#wechat_redirect";
       gzhUrl=gzhUrl.replace('APPID',config.appid).replace('REDIRECT_URI',paramUrl);
       let resultData={
           url:gzhUrl
       };
       let result={
           code:1,
           msg:'获取成功',
           data:resultData
       };
       ctx.body=result;
   }catch (e){
       let result={code:3,msg:e.message};
       ctx.body=result;
   }
});

/**
 * @api {post} /api/gzh/login/getUserInfo 根据code获取用户信息
 * @apiName getUserInfo
 * @apiGroup login
 *
 * @apiParam {String} code 用户授权登录时获取的code
 *
 * @apiSuccess {Object[]} data
 * @apiSuccess {String} data.openid 用户openid
 * @apiSuccess {String} data.nickname 用户昵称
 * @apiSuccess {String} data.headimgurl 用户头像
 *
 */
router.post('/getUserInfo',async (ctx)=>{
    try{
        let param=ctx.request.body;
        let code=param.code;
        if(!code){
            let result={
                code:4,
                msg:'参数不全'
            };
            ctx.body=result;
            return;
        }
        //获取openid和access_token
        let result1=await gzhUtil.getToken(code);
        if(!result1.openid||!result1.access_token){
            const result={
                code:2,
                msg:'获取openid和access_token失败'
            };
            ctx.body=result;
            return;
        }
        //获取用户信息
        let result2=await gzhUtil.getUserInfo(result1.openid,result1.access_token);
        if(!result2.openid){
            const result={
                code:2,
                msg:'获取用户信息失败'
            };
            ctx.body=result;
            return;
        }
        let resultData={
            openid:result2.openid,
            nickname:result2.nickname,
            headimgurl:result2.headimgurl
        };
        let result={
            code:1,
            msg:'获取成功',
            data:resultData
        };
        ctx.body=result;
    }catch (e){
        let result={code:3,msg:e.message};
        ctx.body=result;
    }
});

/**
 * @api {post} /api/gzh/login/getSingPackage 获取签名信息
 * @apiName getSingPackage
 * @apiGroup login
 *
 * @apiParam {String} url 传递当前页面url
 *
 * @apiSuccess {Object[]} data
 * @apiSuccess {String} data.appId 公众号唯一标识
 * @apiSuccess {String} data.timestamp 生成签名的时间戳
 * @apiSuccess {String} data.nonceStr 生成签名的随机串
 * @apiSuccess {String} data.signature 签名
 *
 */
router.post('/getSingPackage',async (ctx)=>{
    try{
        let param=ctx.request.body;
        let paramUrl=param.url;
        if(!paramUrl){
            let result={
                code:4,
                msg:'参数不全'
            };
            ctx.body=result;
            return;
        }
        //获取access_token
        let tokenResult=await gzhUtil.getBaseToken();
        if(!tokenResult.access_token){
            const result={
                code:2,
                msg:'获取token失败'
            };
            ctx.body=result;
            return;
        }
        //获取jsapi_ticket
        let result1=await gzhUtil.getTicket(tokenResult.access_token);
        if(result1.errcode!==0){
            const result={
                code:2,
                msg:'获取jsapi_ticket失败'
            };
            ctx.body=result;
            return;
        }
        //获取签名信息
        let result2=await gzhUtil.getSignature(result1.ticket,paramUrl);
        if(!result2.signature){
            const result={
                code:2,
                msg:'获取签名失败'
            };
            ctx.body=result;
            return;
        }
        let result={
            code:1,
            msg:'获取成功',
            data:{
                noncestr:result2.noncestr,
                timestamp:result2.timestamp,
                signature:result2.signature,
                appId:config.appid
            }
        };
        ctx.body=result;
    }catch (e){
        let result={code:3,msg:e.message};
        ctx.body=result;
    }
});

module.exports=router;